package com.looookme.shaozhuo.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

/**
 * Created by Shaozhuo on 2015/9/8.
 */
public class ActivityForResult extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if(requestCode == FORRESULT && resultCode == RESULT){
            Toast.makeText(ActivityForResult.this, intent.getExtras().get("RESULT").toString(), Toast.LENGTH_SHORT).show();
        }
    }

    private static final int FORRESULT = 0x11;
    private static final int RESULT = 0x22;
    private void initView() {
        LinearLayout ll = new LinearLayout(this);
        ll.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        ll.setOrientation(LinearLayout.VERTICAL);
        setContentView(ll);

        final EditText editTextNum1 = new EditText(this);
        editTextNum1.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        editTextNum1.setHint("Please input the no_1");
        ll.addView(editTextNum1);

        final EditText editTextNum2 = new EditText(this);
        editTextNum2.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        editTextNum2.setHint("Please input the no_2");
        ll.addView(editTextNum2);

        Button btn = new Button(this);
        btn.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        btn.setText("Start Activity ForResult");
        ll.addView(btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!"".equals(editTextNum1.getText().toString()) && !"".equals(editTextNum2.getText().toString())){
                    Intent intent = new Intent(ActivityForResult.this, ActivityForResult_2.class);

                    Bundle bundle = new Bundle();
                    bundle.putString("NUM_1", editTextNum1.getText().toString());
                    bundle.putString("NUM_2", editTextNum2.getText().toString());
                    intent.putExtras(bundle);
                    startActivityForResult(intent, FORRESULT);
                }else{
                    Toast.makeText(ActivityForResult.this, "Input data is Empty", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
